//
//  JKScreenShotTool.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/25.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "JKScreenShotTool.h"

@implementation JKScreenShotTool
/** 控件截图 */
+ (UIImage *)takeScreenShotWithlayer:(CALayer *)layer{
    //1、开启位图上下文
    UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, [UIScreen mainScreen].scale);
    
    //2、当前控制器的view画在位图上下文
    //render 渲染  layer只能渲染
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    layer.contents = (id)nil;
    //3、获取图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //4、结束位图编辑
    UIGraphicsEndImageContext();
    return newImage;
    
    /** 下面的方法截图的倒着并反着的
    size_t width = layer.bounds.size.width;
    size_t height = layer.bounds.size.height;
    
    unsigned char *imageBuffer = (unsigned char *)malloc(width*height*4);
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef imageContext =
    CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
                          kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
    
    CGColorSpaceRelease(colourSpace);
    
    [layer renderInContext:imageContext];
    
    CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
    
    UIImage *newImage = [UIImage imageWithCGImage:outputImage];
    
    CGImageRelease(outputImage);
    CGContextRelease(imageContext);
    free(imageBuffer);
    
    NSData *imgData = UIImagePNGRepresentation(newImage);
    [imgData writeToFile:@"/Users/albert/Desktop/screenshot.png" atomically:YES];
    
    return newImage; */
}
@end
